﻿
@{
    ViewBag.Title = "角色管理";
}

<div class="row col-md-12 ml-0 vh-3">
    <strong class="font-rfs">角色管理</strong>
</div>
<div class="row col-md-12 ml-0 min-vh-90">
    <div class="row col-md-12 min-vh-90" style="padding-right:0">
        <div class="row col-md-12 vh-5">
            <button id="RolesManager-addRole" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
            <button id="RolesManager-delRole" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
        </div>
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 min-vh-85 pre-scrollable">
            <table id="RolesManager-Table" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>角色名</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal" id="RolesManager-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">添加角色</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <div class="col-3 pr-0">
                        <span class="input-group-text font-rfs border-radius-noright noRightBorder">角色名*</span>
                    </div>
                    <div class="col-9 pl-0">
                        <input id="RolesManager-Modal-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
                    </div>
                </div>
                <div class="text-right">
                    <button id="RolesManager-Modal-confirm" class="btn btn-primary font-rfs">确定</button>
                    <button id="RolesManager-Modal-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="RolesManager-DelRole" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中角色吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="RolesManager-DelRole-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="RolesManager-DelRole-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var RolesManager_RoleID = "";

        $(document).ready(function () {
            LoadRoles();
        });

        function LoadRoles() {
            var columns = [{ field: "id", visible: false }, { field: "name" }];

            RolesManager_RoleID = "";
            $('#RolesManager-Table').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#RolesManager-Table').bootstrapTable({
                method: 'post',
                url: "GetRoles",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                uniqueId: "userName",          // 设置主键
                locale: "zh-CN",
                columns: columns,
                silent: true,  //刷新事件必须设置
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                    };
                    return data;
                },

                queryParamsType: "limit",
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 15,   // 默认页大小
                pageList: [10, 20, 30, 50, 100],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                onClickRow: function (row, $element, field) {
                    $("#RolesManager-Table").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    RolesManager_RoleID = row.id;
                },
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#RolesManager-Table').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        };

        $("#RolesManager-addRole").click(function () {
            $("#RolesManager-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#RolesManager-Modal-confirm").click(function () {
            var name = $("#RolesManager-Modal-Name").val().replace(/[\s]/g, '');
            if (name.length == 0) {
                alert("角色名不能为空！");
                return;
            }

            $.post("AddRole", {
                RoleName: name
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#RolesManager-Modal").modal('hide');
                    LoadRoles();
                } else {
                    alert(data);
                }
            });
        });

        $("#RolesManager-delRole").click(function () {
            if (RolesManager_RoleID == "") {
                alert("请先选择需要删除的角色！");
                return;
            }

            $("#RolesManager-DelRole").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#RolesManager-DelRole-delete").click(function () {
            if (RolesManager_RoleID == "") {
                alert("请先选择需要删除的角色！");
                return;
            }

            $.post("RemoveRole", {
                RoleId: RolesManager_RoleID
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#RolesManager-DelRole").modal('hide');
                    LoadRoles();
                } else {
                    alert(data);
                }
            });
        });

    </script>
}
